using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Analysis.Standard;


namespace Bots.Index
{
    public class IndexOptimizateTasker : MutexTask
    {
        public override void Run()
        {
            if (!SearchConfig.IndexOptimize) return;
            logger.Info("IndexOptimizateTasker Running"); 
            NameValueCollection dirs = SearchConfig.IndexDirectorys;
            for (int i = 0; i < dirs.Count; i++) {
                string dir = dirs[i];
                bool create = (!IndexReader.IndexExists(dir));
                if (!create) continue;
                IndexWriter indexWriter = new IndexWriter(dir, new StandardAnalyzer(), false);
                indexWriter.Optimize();
                indexWriter.Close();
            }
            SearchConfig.IndexOptimize = false;
            logger.Info("IndexOptimizateTasker Finish");
        }

        public override void Suspend()
        {
            logger.Info("IndexOptimizateTasker start Suspend");
            base.Suspend();
        }

        public override void Resume()
        {
            logger.Info("IndexOptimizateTasker start Resume");
            base.Resume();
        }
    }
}
